package com.yulebaby.callsenter.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.charts.BarChart;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.ActualTranProgressBean;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.ShowResultEvent;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.RequestTranProgressTask;
import com.yulebaby.callsenter.ui.activity.AddTodayTargetActivity;
import com.yulebaby.callsenter.ui.base.BaseFragment;
import com.yulebaby.callsenter.ui.view.ActualCountColumn;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Author: Zhangbo
 * Time: 2017/1/21
 */
public class StatisticalTargetFragment extends BaseFragment {


    @BindView(R.id.bc_tran)
    BarChart mBcTran;

    private ActualCountColumn mActualCountColumn;

    public static StatisticalTargetFragment newInstance() {
        Bundle args = new Bundle();
        StatisticalTargetFragment fragment = new StatisticalTargetFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trantarget, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mActualCountColumn = new ActualCountColumn(mBcTran);
        requestData();

    }

    /**
     * 请求数据
     */
    private void requestData() {
        RequestTranProgressTask.doRequestActualTranProgress(mActivity, new HttpCallback<List<ActualTranProgressBean>>() {
            @Override
            public void onSuccess(List<ActualTranProgressBean> result) {
                super.onSuccess(result);
                processResult(result);
                EventBus.getDefault().post(EventFactory.CreateShowResultEvent(ShowResultEvent.SuccessEvent));
            }

            @Override
            public void onFailParam(String info) {
                super.onFailParam(info);
                EventBus.getDefault().post(EventFactory.CreateShowResultEvent(ShowResultEvent.FailEvent));
            }
        });
    }

    private void processResult(List<ActualTranProgressBean> result) {
        mActualCountColumn.setData(result);
    }

    @OnClick(R.id.btn_addtarget)
    public void onClick() {
        startActivity(new Intent(mActivity, AddTodayTargetActivity.class));
    }
}
